from flask import Flask, render_template, request
import query
from flask import Blueprint
from MySQL import mysql_c
from flask_cors import CORS

task_api = Blueprint('task_api', __name__)
CORS(task_api)	# 注册CORS

#新建一轮作业
@task_api.route('/task_new', methods=['get', 'post'])
def newround():
    if (request.method == 'POST'):
        cid = request.json.get('classid') #班级id
        tasknote = request.json.get('tasknote') #新一轮作业的信息
    else:
        cid = request.values.get('classid')
        tasknote = request.values.get('tasknote')

    if cid:
        backinf = query.class_new_round(mysql_c, cid,tasknote)
        if backinf[0] == -1:
            return {"msg": "新增失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "新增成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

#搜索某一课堂的任务
@task_api.route('/task_search', methods=['get', 'post'])
def searchtask():
    if (request.method == 'POST'):
        cid = request.json.get('classid')#课堂id
        number = request.json.get('number')#作业轮数
    else:
        cid = request.values.get('classid')
        number = request.values.get('number')

    if cid:
        if not number:
            number=-1
        backinf = query.task_search(mysql_c, cid,number)
        if backinf[0] == -1:
            return {"msg": "搜索失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "搜索成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

